package com.authine.cloudpivot.ext.applicationservice.ext;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.SingleResponse;
import com.authine.cloudpivot.ext.dto.OrgDTO;
import com.authine.hermes.app.launcher.org.v2.DepartmentV2;
import com.authine.hermes.app.launcher.org.v2.SelectionDTO;
import com.authine.hermes.app.launcher.service.ApplicationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.ws.rs.POST;
import javax.ws.rs.Path;

import java.util.stream.Collectors;

import static com.authine.hermes.app.launcher.service.ApplicationServiceFoundation.CUSTOM_SERVICE;

/**
 * @ClassName OrgExtService
 * @Description: 组织扩展服务
 * @Author fj
 * @Date 2021/7/29
 * @Version V1.0
 **/
@Path("OrgExtService")
@Slf4j
public class OrgExtService extends ApplicationService{
    @Path("customTree")
    @POST
    @Operation(summary = "组织树：选人控件", tags = CUSTOM_SERVICE)
    public SingleResponse<SelectionDTO> customTree(@RequestBody OrgDTO orgDTO) {
        log.info("=========OrgDTO:{}", orgDTO);
        String deptIds = "";
        if (!StringUtils.isEmpty(orgDTO.getTargetOrgId())) {
            MultiResponse<DepartmentV2> departmentByParentId = this.orgServiceV2.getDepartmentByParentId(orgDTO.getTargetOrgId());
            log.info("=========根据父级部门id查询子部门返回结果：{}", departmentByParentId);
            deptIds = departmentByParentId.getData().stream().map(DepartmentV2::getId).collect(Collectors.joining(","));
            log.info("=========返回结果转逗号分隔字符串：{}", deptIds);
        }
        SingleResponse<SelectionDTO> treeResult = this.orgServiceV2.selectionTree(orgDTO.getAppCode(), orgDTO.getUserId(), deptIds,
                orgDTO.getOrgId(), orgDTO.getExcludeOrgId(), orgDTO.getExcludeDeptIds(), orgDTO.getExcludeUserIds(), orgDTO.getIsFilterSelfMaintenance(),
                orgDTO.getStrategy(), orgDTO.getSearchType(), orgDTO.getPageNum(), orgDTO.getPageSize(), orgDTO.getResourceCode());
        log.info("=======selectionDTO={}", treeResult);
        return treeResult;
    }

    @Path("customTreeSearch")
    @POST
    @Operation(summary = "组织树查询 选人控件", tags = CUSTOM_SERVICE)
    public SingleResponse<SelectionDTO> customTreeSearch(@RequestBody OrgDTO orgDTO) {
        log.info("=========OrgDTO:{}", orgDTO);
        SingleResponse<SelectionDTO> treeSearchResult = this.orgServiceV2.selectionTreeSearch(orgDTO.getAppCode(), orgDTO.getKeywork(), orgDTO.getUserId(),
                orgDTO.getDeptIds(), orgDTO.getOrgId(), orgDTO.getExcludeOrgId(), orgDTO.getExcludeDeptIds(), orgDTO.getExcludeUserIds(), orgDTO.getIsFilterSelfMaintenance(),
                orgDTO.getStrategy(), orgDTO.getSearchType(), orgDTO.getPageNum(), orgDTO.getPageSize(), orgDTO.getResourceCode());
        log.info("=======selectionDTO={}", treeSearchResult);
        return treeSearchResult;
    }
}
